/// <summary>
/// UV序列帧
/// </summary>
using UnityEngine;
using System.Collections;

public enum UV_TILE_PLAY_MODE
{
	ONCE,
	LOOP
}

public enum UV_MAT_MODE
{
	WITHOUT,
	WITH,
}

public class UVTileAnimation : MonoBehaviour
{
	public int xTile = 8;
	public int yTile = 8;
	public int tileCount = 64;
	public int fps = 30;
	public UV_TILE_PLAY_MODE playMode = UV_TILE_PLAY_MODE.LOOP;
	public UV_MAT_MODE mode = UV_MAT_MODE.WITHOUT;
	public float startDelay = 0.0f;
	public float stopDelay = 0.0f;
	public float playTime = -1.0f;
	public Material material;
	
	private float curTime = 0.0f;

	private float lastTime = 0.0f;
	private int curTile = 0;
	private bool stop = false;
	private Vector2 tempScale = Vector2.zero;
	private Vector2 tempOffset = Vector2.zero;
	//private Material material = null;
	
	void Start ()
	{
	}
	
	void OnEnable()
	{
		if(GetComponent<Renderer>() == null || GetComponent<Renderer>().material == null)
		{
			enabled = false;
			return ;
		}
		
		Initialize();
		Invoke("StartOnTime", startDelay);
	}
	
	void OnDisable()
	{
	}
	
	private void Initialize()
	{
		if(mode == UV_MAT_MODE.WITHOUT)
		{
			material = GetComponent<Renderer>().material;
		}
		else
		{
			if(null == material)
			{
				return;
			}
		}
		//material = renderer.material;
		
		tempScale.x = 1.0f / (float)xTile;
		tempScale.y = -1.0f / (float)yTile;
		material.mainTextureScale = tempScale;
		
		tempOffset.x = 0.0f;
		tempOffset.y = 0.0f;
		material.mainTextureOffset = tempOffset;
		
		stop = true;
		lastTime = Time.realtimeSinceStartup;
		curTile = 0;
		curTime = 0.0f;
		GetComponent<Renderer>().enabled = false;
	}
	
	// Update is called once per frame
	void Update ()
	{	
		if(playMode == UV_TILE_PLAY_MODE.ONCE)
		{
			if( curTile == (tileCount - 1) )
			{
				Invoke("StopOnTime",stopDelay);
			}
		}
		
		if(playMode == UV_TILE_PLAY_MODE.LOOP)
		{
			if(playTime != -1.0f && playTime > 0.0f)
			{
				curTime += Time.deltaTime;
				if(curTime > playTime)
				{
					curTime = 0.0f;
					Invoke("StopOnTime",stopDelay);
				}
			}
		}
		
		if( !stop )
		{
			_UpdateFrameSequence();
		}
	}
	
	public void StartOnTime()
	{
		stop = false;
		GetComponent<Renderer>().enabled = true;
	}
	
	public void StopOnTime()
	{
		stop = true;
		GetComponent<Renderer>().enabled = false;
	}
	
	private void _UpdateFrameSequence()
	{
		float perFrameTime = 1.0f / fps;
		float curTime = Time.realtimeSinceStartup;
		if( (curTime - lastTime) > perFrameTime )
		{
			curTile++;
			curTile = curTile % tileCount;

			float xTileSize = 1.0f / (float)xTile;
			float yTileSize = -1.0f / (float)yTile;

			float xTileIdx = (int)curTile % (int)xTile;
			float yTileIdx = (int)curTile / (int)xTile;

			float uStart = xTileIdx * xTileSize;
			float vStart = yTileIdx * yTileSize;
			
			tempOffset.x = uStart;
			tempOffset.y = vStart;
			
			tempScale.x = xTileSize;
			tempScale.y = yTileSize;
			
			material.mainTextureScale = tempScale;
			material.mainTextureOffset = tempOffset;
			
			lastTime = curTime;
		}
	}
}